/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch.ui;

import net.cachapa.weightwatch.*;
import java.io.IOException;
import javax.microedition.midlet.*;

import com.sun.lwuit.*;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.animations.Transition;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.geom.Rectangle;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import java.util.Enumeration;
import java.util.Vector;

public class Application extends MIDlet {

	protected static final int SHOW_MAIN_FORM = 1;
	protected static final int SHOW_ADD_VALUE_FORM = 2;
	protected static final int SHOW_PREFERENCES_FORM = 4;
	protected static final int QUIT = 3;
	protected static final int ADD_VALUE = 5;
	protected static final int SHOW_PLAN_FORM = 6;
	protected static final int SAVE_PLAN = 7;
	protected static final int SHOW_DATABASE_LIST_FORM = 8;
	protected static final int UPDATE_CHARTS = 9;
	protected static final int SAVE_PREFERENCES = 10;
	protected static final int SHOW_UPDATE_FORM = 11;
	protected static final int PERFORM_UPDATE = 12;
	
	private WeightWatch weightWatch;
	private MainForm mainForm;
	private InsertForm insertForm;
	private PlanForm planForm;
	private PreferencesForm preferencesForm;
	private DatabaseListForm databaseListForm;
	private Vector availableLanguages;
	private UpdateForm updateForm;
	private Style titleStyle;
	private Style softbuttonStyle;
	private Transition normalTransition;
	private Transition prefsTransitionIn, prefsTransitionOut;

	public void startApp() {
		Display.init(this);
		weightWatch = new WeightWatch();

		String languageCode = weightWatch.getPreferences().getLanguage();
		Resources r;
		UIManager ui = UIManager.getInstance();
		try {
			r = Resources.open("/Theme.res");
			
			ui.setThemeProps(r.getTheme("WeightWatchTheme"));
			ui.setResourceBundle(r.getL10N("loc", languageCode));
			
			availableLanguages = new Vector();
			Enumeration e = r.listL10NLocales("loc");
			String s;
			while (e.hasMoreElements()) {
				s = (String)e.nextElement();
				availableLanguages.addElement(s);
			}
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		
		titleStyle = ui.getComponentStyle("Title");
		titleStyle.setBgPainter(new BgPainter(true));
		softbuttonStyle = ui.getComponentStyle("SoftButton");
		softbuttonStyle.setBgPainter(new BgPainter(false));
		
		normalTransition = CommonTransitions.createFade(250);
		if(M3G.isM3GSupported()) {
			prefsTransitionIn = Transition3D.createRotation(500, true);
			prefsTransitionOut = Transition3D.createRotation(500, false);
		} else {
			prefsTransitionIn = CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, false, 250);
			prefsTransitionOut = CommonTransitions.createSlide(CommonTransitions.SLIDE_HORIZONTAL, true, 250);
		}

		if (mainForm == null) {
			mainForm = new MainForm(this);
			mainForm.setTitleStyle(titleStyle);
			mainForm.setSoftButtonStyle(softbuttonStyle);
			mainForm.show();
		}
	}

	protected WeightWatch getWeightWatch() {
		return weightWatch;
	}

	public void pauseApp() {
	}

	public void destroyApp(boolean unconditional) {
	}

	protected void performAction(int actionId) {
		switch (actionId) {
			case SHOW_MAIN_FORM:
				if (preferencesForm != null) {
					preferencesForm.setTransitionInAnimator(prefsTransitionIn);
					preferencesForm.setTransitionOutAnimator(prefsTransitionOut);
				}
				mainForm.show();
				break;
				
			case SHOW_ADD_VALUE_FORM:
				if (insertForm == null) {
					insertForm = new InsertForm(this);
					insertForm.setTitleStyle(titleStyle);
					insertForm.setSoftButtonStyle(softbuttonStyle);
					insertForm.setTransitionInAnimator(normalTransition);
					insertForm.setTransitionOutAnimator(normalTransition);
				}
				insertForm.show();
				break;
				
			case SHOW_PLAN_FORM:
				if (planForm == null) {
					planForm = new PlanForm(this);
					planForm.setTitleStyle(titleStyle);
					planForm.setSoftButtonStyle(softbuttonStyle);
				}
				planForm.show();
				break;
				
			case SHOW_PREFERENCES_FORM:
				if (preferencesForm == null) {
					preferencesForm = new PreferencesForm(this);
					preferencesForm.setTitleStyle(titleStyle);
					preferencesForm.setSoftButtonStyle(softbuttonStyle);
					preferencesForm.setTransitionInAnimator(prefsTransitionIn);
				}
				preferencesForm.show();
				preferencesForm.setTransitionInAnimator(CommonTransitions.createFade(250));
				preferencesForm.setTransitionOutAnimator(CommonTransitions.createFade(250));
				break;
				
			case SHOW_DATABASE_LIST_FORM:
				if (databaseListForm == null) {
					databaseListForm = new DatabaseListForm(this);
					databaseListForm.setTitleStyle(titleStyle);
					databaseListForm.setSoftButtonStyle(softbuttonStyle);
				}
				databaseListForm.show();
				break;
				
			case SHOW_UPDATE_FORM:
				if (updateForm == null) {
					updateForm = new UpdateForm(this);
					updateForm.setTitleStyle(titleStyle);
					updateForm.setSoftButtonStyle(softbuttonStyle);
				}
				updateForm.show();
				break;
			
			case QUIT:
				notifyDestroyed();
				break;
		}
	}

	public Vector getAvailableLanguages() {
		return availableLanguages;
	}

	void setLanguage(String selectedLanguage) {
		try {
			Resources r = Resources.open("/Theme.res");
			UIManager.getInstance().setResourceBundle(r.getL10N("loc", selectedLanguage));
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public class BgPainter implements Painter {

		private static final int LOW_COLOR = 0x222222;
		private static final int HIGH_COLOR = 0x404040;
		private static final int BORDER_COLOR = 0x555555;
		private static final int GRADIENT_HEIGHT = 10;
		private Image cache;
		private boolean title;

		public BgPainter(boolean title) {
			this.title = title;
		}

		public void paint(Graphics g, Rectangle rect) {
			Dimension d = rect.getSize();
			int x = rect.getX();
			int y = rect.getY();
			int height = d.getHeight();
			int width = d.getWidth();
			if (cache == null || width != cache.getWidth()) {
				cache = Image.createImage(width, height);
				Graphics imgG = cache.getGraphics();
				imgG.setColor(LOW_COLOR);
				imgG.fillRect(0, 0, width, height);
				if (title)
					imgG.fillLinearGradient(LOW_COLOR, HIGH_COLOR, 0, height - GRADIENT_HEIGHT, width, GRADIENT_HEIGHT, false);
				else
					imgG.fillLinearGradient(HIGH_COLOR, LOW_COLOR, 0, 0, width, GRADIENT_HEIGHT, false);
				imgG.setColor(BORDER_COLOR);
				if (title)
					imgG.drawLine(0, height-1, width, height-1);
				else
					imgG.drawLine(0, 0, width, 0);
			}
			g.drawImage(cache, x, y);
		}
	}
}

